<template>
  <Header :title="{ zh: '大底仓目视管理看板', en: 'Bottom Warehouse Visual Board', index: 5 }" />

  <div class="EchartsContainer">
    <div class="row">
      <div class="item" style="width: 40%">
        <div class="title">日出入库状况</div>
        <div class="echarts" id="main"></div>
      </div>
      <div class="item" style="margin-left: 10px">
        <div class="title" style="text-align: start">成型库存明细</div>
        <el-table
          ref="tab"
          border
          class="table"
          :data="tableData"
          style="width: 100%; height: 100%"
        >
          <el-table-column align="center" prop="custom" label="品牌" />
          <el-table-column align="center" prop="lotNo" label="指令号" />
          <el-table-column align="center" prop="orderQty" label="订单数量" />
          <el-table-column align="center" prop="inQty" label="欠数" />
          <el-table-column align="center" prop="outQty" label="库存数" />
          <!-- <el-table-column align="center" prop="ioDate" label="库存明细 " /> -->
        </el-table>
      </div>
    </div>

    <div class="row" style="margin-top: 10px">
      <div class="item" style="width: 40%">
        <div class="title">月出入库状况</div>
        <div class="echarts" id="main2"></div>
      </div>
      <div class="item" style="margin-left: 10px">
        <div class="title">月出入库状况</div>
        <div class="echarts" id="main3"></div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import Header from '../components/Header.vue'
import * as echarts from 'echarts'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { tr } from 'element-plus/es/locales.mjs'

let rollTimer = ref() as any

const tab = ref<HTMLElement>() as any
const autoRoll = (stop: any) => {
  if (stop) {
    clearInterval(rollTimer.value)
    return
  }
  // 这里的 tab 是上方 table 表格绑定的ref值
  const divData = tab.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  rollTimer.value = setInterval(() => {
    divData.scrollTop += 1
    // console.log(divData.scrollTop)
    if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
      divData.scrollTop = 0
    }
  }, 150)
}
onMounted(() => {
  autoRoll(false)
})
onBeforeUnmount(() => {
  autoRoll(true)
})

const showData = ref({
  chart1: {
    data: []
  },
  chart2: {
    data: []
  },
  chart3: {
    data: [
      {
        name: '星期一',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [3112, 2408]
      },
      {
        name: '星期二',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [3792, 3021]
      },
      {
        name: '星期三',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [3629, 2998]
      },
      {
        name: '星期四',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [3508, 2626]
      },
      {
        name: '星期五',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [0, 0]
      },
      {
        name: '星期六',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [0, 0]
      }
    ]
  }
}) as any
const DeptNos = ref([]) as any
const DeptName = ref([]) as any
const day = ref([]) as any
const month = ref([]) as any
const getDeptTypes = async () => {
  try {
    const res = (await request.post<any, any>(`/show/slow/stats`, {
      endDate: '',
      pushUnit: '',
      titleUnit: '日'
    })) as any
    day.value.push(res ? res[0].count : 0)
    day.value.push(res ? res[1].count : 0)
  } catch (error: any) {
    ElMessage.error(error)
  }

  // 当月
  try {
    const res = (await request.post<any, any>(`/show/slow/stats`, {
      endDate: '',
      pushUnit: '',
      titleUnit: '月'
    })) as any
    month.value.push(res ? res[0].count : 0)
    month.value.push(res ? res[1].count : 0)
  } catch (error: any) {
    ElMessage.error(error)
  }
}
const getData = async () => {
  // 获取成品库存明细
  const res = (await request.post<any, any>(`/show/slow/list`, {
    pageNum: '1',
    pageSize: '200'
  })) as any
  tableData.value = res.list
}
const chartData = ref([
  {
    CtyQty: [
      1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
      27, 28, 29, 30, 31
    ],
    PreQty: [
      31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8,
      7, 6, 5, 4, 3, 2, 1
    ]
  }
]) as any
const MonthSendingAndReceiving = async () => {
  // 月 产能状况
  try {
    let qerM = (await request.post<any, any>(`/show/slow/stats`, {
      endDate: '',
      pushUnit: '',
      titleUnit: '当月'
    })) as any
    const cDMAP = new Map(
      qerM.map((item: any) => [item.type + item.title.split('/')[1], item.count])
    )
    chartData.value.forEach((item: any, index: number) => {
      item.CtyQty.forEach((item2: any, index2: number) => {
        if (cDMAP.get('入库' + Number(index2 + 1))) {
          chartData.value[index].CtyQty[index2] = cDMAP.get('入库' + Number(index2 + 1))
        } else {
          chartData.value[index].CtyQty[index2] = '0'
        }
      })
      item.PreQty.forEach((item2: any, index2: number) => {
        if (cDMAP.get('出库' + Number(index2 + 1))) {
          chartData.value[index].PreQty[index2] = cDMAP.get('出库' + Number(index2 + 1))
        } else {
          chartData.value[index].PreQty[index2] = '0'
        }
      })
    })
  } catch (error: any) {
    ElMessage.error(error)
  }
}
onMounted(async () => {
  await getDeptTypes()
  await MonthSendingAndReceiving()
  await getData()
  getEchartsDom()
})
const getEchartsDom = () => {
  let chartDom = document.getElementById('main')
  let myChart = echarts.init(chartDom)
  let option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        data: ['当日入库', '当日出库'],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 15000,
        // interval: 3000,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        barWidth: '40%',
        label: {
          show: true,
          position: 'top'
        },
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#88ADFB' },
              { offset: 1, color: '#E3ECFF' }
            ]
          )
        },
        data: day.value
        // data: showData.value.chart1.data
      }
    ]
  }
  option && myChart.setOption(option)

  // 第二张图
  let chartDom2 = document.getElementById('main2')
  let myChart2 = echarts.init(chartDom2)

  let option2 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        data: ['本月入库', '本月出库'],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 1000,
        // interval: 200,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        label: {
          show: true,
          position: 'top'
        },
        barWidth: '30%',
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#FAC858' },
              { offset: 1, color: '#FFF1D1' }
            ]
          )
        },
        data: month.value
        // data: showData.value.chart2.data
      }
    ]
  }
  option2 && myChart2.setOption(option2)

  // 第三张图
  let chartDom3 = document.getElementById('main3')
  let myChart3 = echarts.init(chartDom3)
  let option3 = {
    legend: {
      data: ['当月入库', '当月出库'],
      top: 'bottom'
    },
    grid: {
      left: 50,
      right: 20,
      top: 20,
      bottom: 50
    },
    xAxis: {
      type: 'category',
      data: [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
        26, 27, 28, 29, 30, 31
      ]
    },
    yAxis: {
      type: 'value'
      //max: 12000
    },
    tooltip: {
      trigger: 'axis'
    },
    series: [
      {
        name: '当月入库',
        //data: [9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000, 9000],
        data: chartData.value[0].CtyQty,
        type: 'line',
        label: {
          show: true,
          position: 'top'
        },
        color: '#91CC75'
      },
      {
        name: '当月出库',
        //data: [0, 9882, 10231, 11350, 10825, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        data: chartData.value[0].PreQty,
        type: 'line',
        label: {
          show: true,
          position: 'top'
        },
        color: '#FAC858'
      }
    ]
  }
  option3 && myChart3.setOption(option3)
}

// 表格数据
const tableData = ref([])
onMounted(() => {
  function One() {
    if (location.href.indexOf('#reloaded') == -1) {
      location.href = location.href + '#reloaded'
      location.reload()
    }
  }
  One()
})
</script>

<style scoped lang="less">
.EchartsContainer {
  border-radius: 4px;
  margin-top: 10px;
  height: calc(100vh - 7vh - 40px);
  width: 100%;
}

.row {
  display: flex;
  height: 50%;
  width: 100%;

  .item {
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding-bottom: 10px;
    padding: 0 10px 5px;

    // align-items: center;
    .title {
      font-weight: 900;
      font-size: 1rem;
      margin-top: 10px;
      text-align: center;
      color: black;
    }

    .echarts {
      flex: 1;
    }
  }
}

:deep(thead) {
  .is-center {
    color: white;
    background-color: #ff9940;
  }
}
</style>
